package mn.idax.exchange.otc.web;

import com.alibaba.fastjson.JSONObject;
import mn.idax.exchange.common.bean.ResponseData;
import mn.idax.exchange.otc.constant.CommonConstant;
import mn.idax.exchange.otc.entity.OTCAds;
import mn.idax.exchange.otc.enums.RedisPrefixKeyEnum;
import mn.idax.exchange.otc.service.OTCAdsService;
import mn.idax.exchange.otc.service.OTCSysDictionaryItemService;
import mn.idax.exchange.otc.service.impl.MessageNotifyService;
import mn.idax.exchange.otc.service.impl.RedisService;
import mn.idax.exchange.otc.util.DateUtils;
import mn.idax.exchange.otc.util.QrCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;

@RequestMapping("/otc/api/v1")
@RestController
@ApiIgnore
public class TestController {

    @Autowired
    private OTCAdsService otcAdsService;
    @Autowired
    private MessageNotifyService messageNotifyService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private OTCSysDictionaryItemService otcSysDictionaryItemService;

    @RequestMapping(value = "/redis/del",method = RequestMethod.GET)
    public ResponseData delRedisKey(@RequestParam String key){
        this.redisService.delKey(RedisPrefixKeyEnum.order_cancel_num.getKey() + key);
        return ResponseData.success();
    }



    @RequestMapping(value = "/getDate",method = RequestMethod.GET)
    public ResponseData getDate() throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("date", DateUtils.getDateToStr(new Date(), CommonConstant.DATE_FORMAT_PATTREN));
        jsonObject.put("utcDate", DateUtils.getDateToStr(DateUtils.getUTCNow(), CommonConstant.UTC_DATE_FORMAT_PATTREN));

        return ResponseData.success(jsonObject);
    }

    @RequestMapping(value = "/qrCode",method = RequestMethod.GET)
    public void get(@RequestParam(name = "w",defaultValue = "200",required = false) int width,
                    @RequestParam(name = "h",defaultValue = "200",required = false) int height,
                    @RequestParam(name = "f",defaultValue = "png",required = false) String format,
                    @RequestParam(name = "c",defaultValue = "content") String content,
                    HttpServletResponse response) throws Exception {
        ServletOutputStream out = response.getOutputStream();
        QrCodeUtils.writeToStream(width,height,content,format,out);
        System.out.println("二维码生成完毕，已经输出到页面中。");
    }

    @RequestMapping(value = "/ad/insert",method = RequestMethod.GET)
    public void insertAd(@RequestParam(name = "w",defaultValue = "200",required = false) int width,
                    @RequestParam(name = "h",defaultValue = "200",required = false) int height,
                    @RequestParam(name = "f",defaultValue = "png",required = false) String format,
                    @RequestParam(name = "c",defaultValue = "content") String content,
                    HttpServletResponse response) throws Exception {
        OTCAds otcAds = new OTCAds();
        otcAds.setDescription("1");
        otcAds.setPrice(new BigDecimal("0"));
        otcAds.setRemark("2");
        otcAds.setState(1);
        otcAds.setType(1);
        otcAds.setCreatetime(new Date());
        otcAds.setIscertificate(Boolean.FALSE);
        otcAds.setMaxamount(BigDecimal.ONE);
        otcAds.setMinamount(BigDecimal.ZERO);
        otcAds.setPairname("ETH");
        otcAds.setUpdatetime(new Date());
        otcAds.setWelcomenote("1");
        otcAds.setEndnote("2");
        otcAds.setExpiretime(new Date());
        otcAds.setPairid(1);
        otcAds.setPublishtime(new Date());
        otcAds.setPublishsource(1);
        otcAds.setPublisherid(1);
        otcAds = null;
        this.otcAdsService.insertSelective(otcAds);
    }


}
